//
//  BCProjectDetailShareButton.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/24.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCProjectDetailShareButton.h"

@interface BCProjectDetailShareCircleView : UIView

/**
 *circleLayer
 */
@property (nonatomic, strong) CAGradientLayer *circleLayer;

@end

@implementation BCProjectDetailShareCircleView

- (instancetype)init
{
    self = [super init];
    if (self) {
        //
        self.circleLayer = [CAGradientLayer layer];
        self.circleLayer.type = kCAGradientLayerRadial;
        self.circleLayer.colors = @[(id)[UIColor qmui_colorWithHexString:@"#FF8A66"].CGColor,
                                    (id)[UIColor qmui_colorWithHexString:@"#FF7D61"].CGColor,
                                    (id)[UIColor qmui_colorWithHexString:@"#FE5250"].CGColor];
        [self.layer addSublayer:self.circleLayer];
        self.circleLayer.startPoint = CGPointMake(.5, .5);
        self.circleLayer.endPoint = CGPointMake(1, 1);
        self.circleLayer.locations = @[@(0.0),@(0.66),@(1.0)];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.circleLayer.frame = self.bounds;
}



@end


static CGFloat BCProjectDetailShareButtonRightPadding = 12.0f;

@interface BCProjectDetailShareButton ()

/**
 *maskLayer
 */
@property (nonatomic, strong) CAShapeLayer *maskLayer;


#pragma mark - subs

@property (nonatomic, strong) CAGradientLayer *backgroundLayer;

/**
 *BCProjectDetailShareCircleView
 */
@property (nonatomic, strong) BCProjectDetailShareCircleView *circleView;

/**
 *shareIV
 */
@property (nonatomic, strong) UIImageView *shareIV;


/**
 *shareLabel
 */
@property (nonatomic, strong) QMUILabel *shareLabel;
/**
 *contentFitsLabel
 */
@property (nonatomic, strong) QMUILabel *contentFitsLabel;

@end

@implementation BCProjectDetailShareButton

- (CAShapeLayer *)maskLayer {
    if (!_maskLayer) {
        _maskLayer = [CAShapeLayer layer];
    }
    return _maskLayer;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubs];
    }
    return self;
}

- (void)setupSubs {
    self.backgroundLayer = [CAGradientLayer layer];
    self.backgroundLayer.startPoint = CGPointZero;
    self.backgroundLayer.endPoint = CGPointMake(1, 0);
    self.backgroundLayer.colors = @[(id)[UIColor qmui_colorWithHexString:@"#FF8C64"].CGColor,(id)[UIColor qmui_colorWithHexString:@"#FE5250"].CGColor];
    [self.layer addSublayer:self.backgroundLayer];
    //
    CGFloat circleHeight = [self.class defaultHeight];
    self.circleView = [BCProjectDetailShareCircleView new];
    [self addSubview:self.circleView];
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_left);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(circleHeight);
    }];
    self.circleView.layer.cornerRadius = circleHeight/2;
    self.circleView.layer.masksToBounds = true;
    
    //
    self.shareIV = [UIImageView new];
    self.shareIV.image = LOAD_IMAGE(@"details_shre_reward_image");
    [self addSubview:self.shareIV];
    [self.shareIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.circleView.mas_right);
        make.centerY.mas_equalTo(self.circleView);
    }];
    //
    self.shareLabel = [QMUILabel new];
    self.shareLabel.textColor = [UIColor qmui_colorWithHexString:@"#FFF85B"];
    self.shareLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    self.shareLabel.numberOfLines = 2;
    self.shareLabel.adjustsFontSizeToFitWidth = true;
    self.shareLabel.minimumScaleFactor = 0.6;
    self.shareLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.shareLabel];
    [self.shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-BCProjectDetailShareButtonRightPadding);
        make.centerY.mas_equalTo(self);
    }];
    
    //
    self.contentFitsLabel = [QMUILabel new];
    self.contentFitsLabel.font = self.shareLabel.font;
    
}

- (void)setShareTitle:(NSString *)shareTitle {
    if ([shareTitle isEqualToString:_shareTitle]) {
        return;
    }
    _shareTitle = shareTitle;
    //
    self.shareLabel.text = self.contentFitsLabel.text = shareTitle;
    CGSize labelFitsSize = [self.contentFitsLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
    //
    CGFloat labelWidth = labelFitsSize.width/2;
    CGFloat circleWidth = [self.class defaultHeight]/2;
    CGFloat ivWidth = CGRectGetWidth(self.shareIV.frame)/2;
    CGFloat totalWidth = circleWidth+ivWidth+8.0+labelWidth+BCProjectDetailShareButtonRightPadding;
    //
    [self.shareLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(labelWidth);
    }];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(totalWidth);
    }];
    [self layoutIfNeeded];
    [self layoutSubviews];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundLayer.frame = self.bounds;
    //
    [self setMaskLayerPath];
}


- (void)setMaskLayerPath {
    self.maskLayer.frame = self.bounds;
    //
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(width, 0)];
    [path addLineToPoint:CGPointMake(width, height)];
    [path addLineToPoint:CGPointMake(height/2, height)];
    [path addArcWithCenter:CGPointMake(height/2, height/2) radius:height/2 startAngle:M_PI/2 endAngle:M_PI+M_PI/2 clockwise:true];
    [path closePath];
    self.maskLayer.path = path.CGPath;
    //
    self.layer.mask = self.maskLayer;
}


+ (CGFloat)defaultHeight {
    return 38.0f;
}

@end
